package com.hermes.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.hermes.biz.common.model.user.UserDO;
import com.hermes.biz.manager.user.UserOperateManager;
import com.hermes.common.util.ParameterParserUtil;
import com.hermes.constant.ConstantParams;
import com.hermes.util.file.FileUtil;
import com.hermes.util.file.ImgProducer;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UploadFileAction  extends ActionSupport implements ModelDriven{
	public UserOperateManager userOperateManager;
	
	public UserDO userDO;
	
	private UploadFileActionForm form = new UploadFileActionForm();
	 public Object getModel(){
	  return form;
	 }
	 public String execute() throws Exception {
		 //
		 
		 String userid = ParameterParserUtil.getParameter("userid");
		 String rootPath = ServletActionContext.getRequest().getRealPath("/");
		 File upedFile = upload(rootPath);
		 String upedFilePath = upedFile.getPath();
		 String fileSuffix = StringUtils.substringAfterLast(upedFilePath, ".");
		 
		 String tmpfile = rootPath+"images\\header\\"+"tmp."+fileSuffix.toUpperCase();
		 //测试小图生产
		 //ImgProducer.createSmallSquarePic(rootPath+"images\\item\\tag1");
		 boolean rename = upedFile.renameTo(new File(tmpfile));
		 //reduce to 26*26  _ is part the user id from the file
		 FileUtil.reduceImg(tmpfile, rootPath+"images\\header\\"+"head_"+userid+"."+fileSuffix,ConstantParams.head_pic_size , ConstantParams.head_pic_size );
		 //delete src file
		 FileUtil.deleteFile(tmpfile);
		 //set logoyet
		 userDO = new UserDO();
		 userDO.setHeadPic("images\\header\\head_"+userid+"."+fileSuffix);
		 userDO.setLogoyet(1);
		 userDO.setUserId(Long.valueOf(userid));
		 boolean updateLogoResult = userOperateManager.updateUserLogo(userDO);
		 return SUCCESS;
	 }
	 
	 public File upload(String rootPath){
	  File file   = new File(rootPath+"\\images\\header\\"+form.getUploadifyFileName());	  
	  System.out.println(form.getUsername());
	  System.out.println(form.getTrueName());
	  try{
	   copyToFile(form.getUploadify(), file);
	  
	   ServletActionContext.getResponse().getWriter().write("true");
	  }catch (Exception e) {
	   e.printStackTrace();
	   
	   try {
	    ServletActionContext.getResponse().getWriter().write("false");
	   } catch (IOException e1) {
	    e1.printStackTrace();
	   }
	  }
	  return file;
	 }
	 
	 private void copyToFile(File from ,File to)throws Exception{
	  FileInputStream in = new FileInputStream(from);
	  FileOutputStream out = new FileOutputStream(to);
	  byte[] bs = new byte[10240];
	  int c = 0;
	  while((c=in.read(bs))!=-1){
	   out.write(bs,0,c);
	  }
	  in.close();
	  out.flush();
	  out.close();
	 }
	 
	 //getters and setters
	 public UserOperateManager getUserOperateManager() {
		 return userOperateManager;
	 }
	 public void setUserOperateManager(UserOperateManager userOperateManager) {
		 this.userOperateManager = userOperateManager;
	 }
	 
}
